//
//  ViewController.m
//  GCDOperation
//
//  Created by 崔宝印 on 2018/8/7.
//  Copyright © 2018年 koolearn. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;

@end

@implementation ViewController

BYSingletonM(ViewController);

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self operationQueueImage];
}

- (void)gcdDelay
{
    NSLog(@"%s",__func__);
    //延迟两秒执行
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        NSLog(@"%@",[NSThread currentThread]);
    });
}

- (void)gcdOnce
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSLog(@"app运行期间只会执行一次");
    });
}

- (void)gcdQuickTraversal
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    int count = 10;
    dispatch_apply(count, queue, ^(size_t i) {
        NSLog(@"序号：%zd 线程：%@",i, [NSThread currentThread]);
    });
}


/**
 栅栏函数 对于GlobalQueue不起作用
 */
- (void)gcdBarrirMethod
{
    dispatch_queue_t queue = dispatch_queue_create("barrierQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog(@"1---%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2---%@",[NSThread currentThread]);
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"-------");
    });
    dispatch_async(queue, ^{
        NSLog(@"3---%@",[NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4---%@",[NSThread currentThread]);
    });
}


/**
 异步下载3张图片，合成一张图片
 */
- (void)gcdGroup
{
    __block UIImage *imageA;
    __block UIImage *imageB;
    __block UIImage *imageC;

    dispatch_group_t group = dispatch_group_create();
//    dispatch_queue_t queue = dispatch_queue_create("downloadQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //下载图片1
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载imageA");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747507657&di=69488a0e6eaa39a1f0eac645488d905f&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D3372718970%2C2563243107%26fm%3D214%26gp%3D0.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageA = [UIImage imageWithData:data];
        NSLog(@"imageA下载完成");
    });
    
    //下载图片2
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载imageB");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747647912&di=409bb137f7f38c843b05e9b8c32b4753&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ftransform%2F20150120%2F6L8n-awzunex9189402.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageB = [UIImage imageWithData:data];
        NSLog(@"imageB下载完成");
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载imageC");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747660443&di=82a3280346e7cbd44633f56ddbd20a13&imgtype=jpg&src=http%3A%2F%2Fimg2.imgtn.bdimg.com%2Fit%2Fu%3D3294969647%2C3256858866%26fm%3D214%26gp%3D0.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageC = [UIImage imageWithData:data];
        NSLog(@"imageC下载完成");
    });


    dispatch_group_notify(group, queue, ^{
        NSLog(@"开始合成图片");

        //1:创建上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        //2:画图1
        [imageA drawInRect:CGRectMake(0, 0, 100, 100)];
        //3:画图2
        [imageB drawInRect:CGRectMake(0, 100, 100, 100)];
        //4:画图3
        [imageC drawInRect:CGRectMake(100, 0, 100, 200)];
        //5:根据上下文生产新图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        //6:回收上下文
        UIGraphicsEndImageContext();
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.myImageView.image = image;
        });
    });
}


/**
 使用NSOperationQueue 异步下载三张图片，并合成一张
 */
- (void)operationQueueImage
{
    __block UIImage *imageA;
    __block UIImage *imageB;
    __block UIImage *imageC;
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSBlockOperation *operationA = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载imageC");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747660443&di=82a3280346e7cbd44633f56ddbd20a13&imgtype=jpg&src=http%3A%2F%2Fimg2.imgtn.bdimg.com%2Fit%2Fu%3D3294969647%2C3256858866%26fm%3D214%26gp%3D0.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageC = [UIImage imageWithData:data];
        NSLog(@"imageC下载完成");
    }];
    [queue addOperation:operationA];

    NSBlockOperation *operationB = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载imageB");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747647912&di=409bb137f7f38c843b05e9b8c32b4753&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Ftransform%2F20150120%2F6L8n-awzunex9189402.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageB = [UIImage imageWithData:data];
        NSLog(@"imageB下载完成");
    }];
    [queue addOperation:operationB];

    NSBlockOperation *operationC = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"下载imageA");
        NSString *path = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533747507657&di=69488a0e6eaa39a1f0eac645488d905f&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D3372718970%2C2563243107%26fm%3D214%26gp%3D0.jpg";
        NSURL *url = [NSURL URLWithString:path];
        NSData *data = [NSData dataWithContentsOfURL:url];
        imageA = [UIImage imageWithData:data];
        NSLog(@"imageA下载完成");
    }];
    [queue addOperation:operationC];

    NSBlockOperation *combineOperation = [NSBlockOperation blockOperationWithBlock:^{
        //1:创建上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        //2:画图1
        [imageA drawInRect:CGRectMake(0, 0, 100, 100)];
        //3:画图2
        [imageB drawInRect:CGRectMake(0, 100, 100, 100)];
        //4:画图3
        [imageC drawInRect:CGRectMake(100, 0, 100, 200)];
        //5:根据上下文生产新图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        //6:回收上下文
        UIGraphicsEndImageContext();
        
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            self.myImageView.image = image;
        }];
        [[NSOperationQueue mainQueue] addOperation:operation];
    }];
    [combineOperation addDependency:operationA];
    [combineOperation addDependency:operationB];
    [combineOperation addDependency:operationC];
    
    [queue addOperation:combineOperation];
}
@end
